package pub

import (
	"github.com/gin-gonic/gin"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

var db *gorm.DB

// 初始化数据库连接
func InitDb() {
	var err error
	db, err = gorm.Open(sqlite.Open("dha.db"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
}

func DB() *gorm.DB {
	if db == nil {
		InitDb()
	}

	if gin.Mode() != gin.ReleaseMode {
		db = db.Debug()
	}

	return db
}
